import { PagingInfo } from "./../interface/common.interface";
import { DEFAULT_PAGE_INDEX, DEFAULT_PAGE_SIZE } from "./../constants/index";
import { Provide } from "@midwayjs/decorator";
import BaseService from "../common/base.service";
import { UserModel, UserEntity } from "../entity/user.entity";

@Provide('userService')
export default class UserService extends BaseService {
  async getUserById(uid: string): Promise<UserEntity> {
    const user = await UserModel.findOne({ uid }, { _id: 0 }).lean();
    return user;
  }

  async getUsers(pi: number = DEFAULT_PAGE_INDEX, ps: number = DEFAULT_PAGE_SIZE): Promise<{ users: UserEntity[], paging: PagingInfo }> {
    const query = {};
    const count = await UserModel.getCount(query);
    const paging: PagingInfo = { pi, ps, count };
    const users = await UserModel.findByPaging<UserEntity>(query, paging);
    return { users, paging }
  }

}